import router from '../router';

// 导出vuex分发动作函数

// 文章动作
export const post = ({
    commit,
    state
}, {
    article,
    articleId
}) => {
    let articles = state.articles

    if (!Array.isArray(articles)) articles = []

    // 存在文章对象则修改或新增
    if (article) {
        const uid = 1
        const {
            title,
            content
        } = article

        const date = new Date()

        // 不存在文章id，表明是执行新增动作
        if (articleId === undefined) {
            const lastArticle = articles[articles.length - 1]

            if (lastArticle) {
                articleId = parseInt(lastArticle.articleId) + 1
            } else {
                articleId = articles.length + 1
            }
            // 不存在id则增加
            articles.push({
                uid,
                articleId,
                title,
                content,
                date
            })
        } else {
            // for...of 迭代对象，只针对当前对象上的属性迭代不涉及原型
            for (let article of articles) {
                if (parseInt(article.articleId) === parseInt(articleId)) {
                    let index = articles.indexOf(article)
                    // 解包合并得到修改后的文章
                    article = {
                        ...article,
                        ...{
                            title,
                            content
                        }
                    }
                    // 替换修改
                    articles.splice(index, 1, article)
                    break
                }
            }
        }
        commit('UPDATE_ARTICLES', articles)
        router.push({
            name: 'Content',
            params: {
                articleId,
                showMsg: true
            }
        })
    } else {
        // 不存在文章对象，表明删除文章
        for (let article of articles) {
            if (parseInt(article.articleId) === parseInt(articleId)) {
                articles.splice(articles.indexOf(article), 1)
                break
            }
        }
        // 提交到状态
        commit('UPDATE_ARTICLES', articles)

        router.push({
            name: 'Home',
            params: {
                showMsg: true
            }
        })
    }
}


// 点赞动作
export const like = ({
    commit,
    state
}, {
    articleId,
    isAdd
}) => {
    // 取仓库内的文章
    let articles = state.articles
    // 点赞用户列表
    let likeUsers = []
    // 默认用户为1
    const uid = 1

    if (!Array.isArray(articles)) articles = []
    for (let article of articles) {
        if (parseInt(article.articleId) === parseInt(articleId)) {
            // 更新点赞用户列表
            likeUsers = Array.isArray(article.likeUsers) ? article.likeUsers : likeUsers

            if (isAdd) {
                // 是否已赞
                // some() 方法测试是否至少有一个元素通过由提供的函数实现的测试
                const isAdded = likeUsers.some(likeUser => parseInt(likeUser.uid) === uid)

                // 未点赞用户则加入点赞用户对象列表
                if (!isAdded) {
                    likeUsers.push({
                        uid
                    })
                }
            } else {
                // 点赞过的则将其取消，剃除
                for (let likeUser of likeUsers) {
                    if (parseInt(likeUser.uid) === uid) {
                        // 删除点赞用户
                        likeUsers.splice(likeUsers.indexOf(likeUser), 1)
                        break
                    }
                }
            }

            // 更新文章点赞用户列表 
            article.likeUsers = likeUsers
            break
        }
    }

    // 提交更新所有的文章
    commit('UPDATE_ARTICLES', articles)
    return likeUsers
}


// 评论动作
export const comment = ({
    commit,
    state
}, {
    articleId,
    comment,
    commentId
}) => {
    let articles = state.articles

    // 评论列表
    let comments = []

    if (!Array.isArray(articles)) articles = []

    for (let article of articles) {
        // 寻找评论的文章
        if (parseInt(article.articleId) === parseInt(articleId)) {
            // 更新评论
            comments = Array.isArray(article.comments) ? article.comments : comments

            // 存在评论对象
            if (comment) {
                // 获取用户传入的评论内容，设置用户 ID 的默认值为 1
                const {
                    uid = 1, content
                } = comment
                const date = new Date()

                // 新增评论
                if (commentId === undefined) {
                    const lastComment = comments[comments.length - 1]

                    // 新建评论ID
                    if (lastComment) {
                        commentId = parseInt(lastComment.commentId) + 1
                    } else {
                        commentId = comments.length + 1
                    }

                    // 在已有的评论列表中增加新评论
                    comments.push({
                        uid,
                        commentId,
                        content,
                        date
                    })
                } else {
                    // 修改评论
                    for (let comment of comments) {
                        if (parseInt(comment.commentId) === parseInt(commentId)) {
                            // let index = comments.indexOf(comment)
                            comment.content = content
                            // 替换变更评论内容
                            // comments.splice(index, 1,comment)
                            break
                        }
                    }
                }
            } else {
                // 不存在评论内容时，删除评论
                for (let comment of comments) {
                    if (parseInt(comment.commentId) === parseInt(commentId)) {
                        comments.splice(comments.indexOf(comment), 1)
                        break
                    }
                }
            }
            // 更新文章的评论列表
            article.comments = comments
            break
        }
    }

    // 更新commit同步数据状态 
    commit('UPDATE_ARTICLES', articles)
    return comments
}